package cn.com.joycode.nimble.core.message;

import cn.com.joycode.nimble.core.error.NbErrorCommon;

/**
 * @author Max Woods
 * @since 1.0
 */

class NbMsgDefaultBuilder extends AbstractNbMsgBuilder {

    private NbMsgType type;

    private String spec;

    private String ver;

    public NbMsgDefaultBuilder(NbMsgType type, String spec, String ver) {
        this.type = type;
        this.spec = spec;
        this.ver = ver;
    }

    @Override
    public NbMsg build() {
        return new NbMsg(type, spec, ver);
    }

    @Override
    public NbMsg buildRequest() {
        return new NbMsg(NbMsgType.REQUEST, spec, ver);
    }

    @Override
    public NbMsg buildResponse() {
        return new NbMsg(NbMsgType.RESPONSE, spec, ver);
    }

    @Override
    public NbMsg buildNotice() {
        return new NbMsg(NbMsgType.NOTICE, spec, ver);
    }

    @Override
    public NbMsg buildResponse(NbMsg reqMsg) {
        return buildResponse(reqMsg, true);
    }

    public NbMsg buildResponse(NbMsg reqMsg, Boolean copyReq) {
        NbMsg result = buildResponse();
        result.getHeader().setRef(reqMsg.getHeader().getId());
        if (copyReq) {
            result.putBody(reqMsg.getBody());
        }
        return result;
    }

    @Override
    public NbMsg buildSuccessResponse() {
        NbMsg result = buildResponse();
        result.setMsgError(NbErrorCommon.SUCCESS);
        return result;
    }

    @Override
    public NbMsg buildSuccessResponse(NbMsg reqMsg) {
        NbMsg result = buildResponse(reqMsg);
        result.setMsgError(NbErrorCommon.SUCCESS);
        return result;
    }

    @Override
    public NbMsg buildSuccessResponse(NbMsg reqMsg, boolean copyReq) {
        NbMsg result = buildResponse(reqMsg);
        result.setMsgError(NbErrorCommon.SUCCESS);
        return result;
    }

    @Override
    public NbMsg buildFailResponse() {
        NbMsg result = buildResponse();
        result.setMsgError(NbErrorCommon.FAIL);
        return result;
    }
}
